Skip to main content

Odd Even Linked List

Question

None

Example 1
Input: 1->2->3->4->5->NULL

Output: 1->3->5->2->4->NULL

Solution

all//Odd Even Linked List.py


# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next

class Solution:
def oddEvenList(self, head):
if head is None:
return head
odd = head
even = head.next
evenhead = even
while even and even.next:
odd.next = odd.next.next
even.next = even.next.next
odd = odd.next
even = even.next
odd.next = evenhead
return head